<template>
  <div>
    <!-- banner -->
    <div class="home-banner" :style="cover">
      <div class="banner-container">
        <!-- 网站标题 -->
        <h1 class="blog-title animated zoomIn">
          {{ config.webTitle }}
        </h1>
      </div>
      <!-- 向下滚动 -->
      <div class="scroll-down" @click="scrollDown">
        <v-icon color="#fff" class="scroll-down-effects">mdi-chevron-down</v-icon>
      </div>
    </div>
    <!-- 主页文章 -->
    <v-row class="home-container">
      <v-col md="9" cols="12">
        <v-card v-if="noticeContent">
          <div style="display: flex;margin-top: 20px;padding: 1rem 1rem">
            <v-icon style="color: red">mdi-bell-outline</v-icon>
            <span style="margin-left: 10px">{{ noticeContent }}</span>
          </div>
        </v-card>
        <v-card class="animated zoomIn article-card" style="border-radius: 12px 8px 8px 12px" v-for="(item, index) of articleList" :key="item.articleId">
          <!-- 文章封面图 -->
          <div :class="isRight(index)">
            <router-link :to="'/articles/' + item.articleId">
              <v-img class="on-hover" width="100%" height="100%" :src="item.cover"/>
            </router-link>
          </div>
          <!-- 文章信息 -->
          <div class="article-wrapper">
            <div style="line-height:1.4">
              <div style="width: 100%">
                <router-link :to="'/articles/' + item.articleId" tag="a" target="_blank">
                  <span style=" display :inline-block ;width: 100%;overflow: hidden;textOverflow: ellipsis;whiteSpace: nowrap"
                        :title="item.title">
                      {{ item.title }}
                  </span>
                </router-link>
<!--                <span v-if=" item.views >= 1000" style="font-size:12px;border-radius:3px;border: 1px solid  #f70;text-align: center">
                  <font style="color: #f70;">千次阅读</font>
                </span>-->
              </div>
            </div>
            <div class="article-info">
              <strong>作者</strong>：
              <router-link :to="'/user/' + item.authorId" tag="a" target="_blank">
                {{ item.authorName }}
              </router-link>
            </div>

            <div class="article-info" v-if="item.organizationList">
              <strong>组织</strong>：
              <router-link v-for="(o, i) in item.organizationList" :key="o.organizationId"
                  :to="'/organization/' + o.organizationId" tag="a" target="_blank">
                {{ o.organizationName }} |
              </router-link>
            </div>

            <div class="article-info">
              <strong>发布于</strong>：
              <v-icon size="14">mdi-calendar-month-outline</v-icon>
              {{ item.createTime }}
            </div>
            <div class="article-info">
              <!-- 文章分类 -->
              <router-link :to="'/categories/' + item.categoryId" tag="a" target="_blank">
                <strong>分类</strong>：
                <v-icon size="14">mdi-inbox-full</v-icon>
                {{ item.categoryName }}
              </router-link>
              <span class="separator">|</span>
              <!-- 文章标签 -->
              <strong>标签</strong>：
              <v-icon size="14">mdi-tag-multiple</v-icon>{{ item.labelName }}
            </div>
            <div class="article-info">
              <!-- 阅读量 -->
              <strong>阅读量</strong>： {{ item.views }}
              <span class="separator">|</span>
              <!-- 点赞量 -->
              <strong>点赞量</strong>： {{ item.likes }}
            </div>
            <!-- 文章摘要 -->
            <div class="article-content" style="width: 100%">
              <span style=" display :inline-block ;width: 100%;overflow: hidden;textOverflow: ellipsis;whiteSpace: nowrap"
                    :title="item.summary">
                      {{ item.summary }}
              </span>
            </div>
          </div>
        </v-card>

        <!--    加载更多    -->
        <el-button type="info" :disabled="moreTitle === '没有更多了...' " round style="margin-left: 50%;margin-top: 10px;" :loading="jiazailoading" @click="more">{{moreTitle}}</el-button>
      </v-col>

      <!-- 博主信息 -->
      <v-col md="3" cols="12" class="d-md-block d-none">
        <div class="blog-wrapper">
          <!-- 网站信息 -->
          <v-card class="right-container" style="margin-top:20px">
            <div class="right-title">
              <i class="iconfont icongengxinshijian" style="font-size:16.8px" />
              <span style="margin-left:10px">阅读量排行榜</span>
            </div>
            <div class="article-list">
              <div class="article-item" v-for="item of viewsArticleList" :key="item.articleId">
                <router-link :to="'/articles/' + item.articleId " tag="a" target="_blank" class="content-cover">
                  <img :src="item.cover" alt="文章封面" />
                </router-link>
                <div class="content">
                  <div style="width: 90%">
                    <router-link :to="'/articles/' + item.articleId" tag="a" target="_blank">
                      <span style=" display :inline-block ;width: 100%;overflow: hidden;textOverflow: ellipsis;whiteSpace: nowrap"
                          :title="item.title">
                        {{ item.title }}
                      </span>
                    </router-link>
                  </div>
                  <div class="content-time">
                    <span style="font-size:12px;border-radius:3px;border: 1px solid  #f70;text-align: center;margin-left: 5px;">
                      <font style="color: #f70;" v-if="item.views > 0">{{ item.views }} 阅读</font>
                  </span>
                  </div>
                </div>
              </div>
            </div>
          </v-card >

          <!-- 分类市场 -->
          <v-card class="right-container" style="margin-top:20px">
            <div class="right-title">
              <i class="iconfont icongengxinshijian" style="font-size:16.8px" />
              <span style="margin-left:10px">分类市场</span>
              <span style="margin-left: 80px;" >
                <el-link :underline="false" @click="goToCategory('/categories')">更多<i class="el-icon-view el-icon--right"></i> </el-link>
              </span>
            </div>
            <div class="article-list">
              <el-tag :style="{ backgroundColor: `${randomColor()}` }" style="margin-left: 10px;"
                      v-for="(item, index) in categories" :key="index">{{ item.categoryName }}</el-tag>
            </div>
          </v-card >




<!--          <v-card class="blog-card animated zoomIn mt-5 big">
            <div class="web-info-title">
              <v-icon size="18">mdi-account</v-icon>
              关注我们
            </div>
            <div class="guanzhu" id="follow-us" ref="follow">
              {{ config.bulletin }}
              <ul>
                <li class="qq">
                  <a :href="
                        'tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=' +
                        config.qqNumber
                    "
                    target="_blank"
                    >2915524550</a
                  >
                </li>
                <li class="gitee">
                  <a href="https://gitee.com/invincible-style" target="_blank">https://gitee.com/invincible-style</a>
                </li>
              </ul>
            </div>
          </v-card>-->
<!--          <v-card class="blog-card animated zoomIn mt-5">
            <div id="he-plugin-standard"></div>
          </v-card>-->
          <!-- 网站信息 -->
          <v-card class="blog-card animated zoomIn mt-5">
            <div class="web-info-title">
              <v-icon size="18">mdi-chart-line</v-icon>
              网站资讯
            </div>
            <div class="web-info">
              <div style="padding:4px 0 0">
                运行时间:<span class="float-right">{{ time }}</span>
              </div>
              <div style="padding:4px 0 0">
                总访问量:<span class="float-right">
                  1000
                </span>
              </div>
              <div style="padding:4px 0 0">
                当前在线人数:<span class="float-right">800</span>
              </div>
            </div>
          </v-card>
        </div>
      </v-col>
    </v-row>
    <!-- 提示消息 -->
    <v-snackbar v-model="tip" top color="#49b1f5" :timeout="2000">
      按CTRL+D 键将本页加入书签
    </v-snackbar>
  </div>
</template>

<script>
import { getWebinfo, systemNotice } from "@/api/system";
import { getArticleList, getViewsArticle, categoryList } from "@/api/article";
import { handlerArticleSummary, handlerArticleTitle } from "../../utils/commonUtil";

export default {
  created() {
    this.fetchWebsiteConfig();
    this.getViewArticle();
    this.getCategoryList();
    this.init();
    //this.timer = setInterval(this.runTime, 1000);
  },
  destroyed() {
    // 销毁监听
    this.socket.onclose = this.close;
  },
  beforeDestroy() {
    clearInterval(this.heartBeat);
  },
  mounted: function() {
    window.WIDGET = {
      CONFIG: {
        layout: "2",
        width: 230,
        height: 300,
        background: "2",
        dataColor: "000000",
        borderRadius: "5",
        key: "4034255d0cc94897b5f745367ccb81f2"
      }
    };
    let script = document.createElement("script");
    script.type = "text/javascript";
    script.src =
      "https://widget.qweather.net/standard/static/js/he-standard-common.js?v=2.0";
    document.getElementsByTagName("head")[0].appendChild(script);
  },

  watch: {
    current(value) {
      this.current = value;
      this.fetchArticleList();
    }
  },
  data: function() {
    return {
      calendarVal: new Date(),
      config: {}, // 网站配置信息
      baseInfo: {},
      current: 1,
      size: 10,
      total: 0,
      path: process.env.VUE_APP_WEBSOCKET_API,
      socket: "",
      onlineCount: 0,
      heartBeat: null,
      formLabelWidth: "80px",
      form: {
        type: 1,
        email: null,
        title: null,
        content: null,
        imgUrl: null
      },
      webSite: {},
      tip: false,
      totalPage: null,
      img: process.env.VUE_APP_IMG_API,
      time: "",
      obj: {
        output: "",
        isEnd: false,
        speed: 300,
        singleBack: false,
        sleep: 0,
        type: "rollback",
        backSpeed: 40,
        sentencePause: true
      },
      articleList: [],
      params: {
        current: 1,
        size: 2
      },
      viewsArticleList: [],
      jiazailoading: false,
      pageNo: 2,
      moreFlag: true,
      moreTitle: "加载更多",
      categories: [],
      noticeContent: ""
    };
  },
  methods: {
    // 加载更多
    more() {
      let param = {
        pageNo: this.pageNo,
        pageSize: 10
      }
      getArticleList(param).then(res => {
        if (res.code === 200) {
          this.articleList = this.articleList.concat(res.data.list);
          const a = Math.ceil(res.data.total / this.size)
          if (a === this.pageNo) {
            this.moreTitle = "没有更多了..."
          }
          this.pageNo = this.pageNo + 1;
        }
      })
    },

    // 分类列表
    getCategoryList() {
      const param = { pageNo: 1, pageSize: 10}
      categoryList(param).then(res => {
        if (res.code === 200) {
          this.categories = res.data.list;
        }
      })
    },

    // 去分类页面
    goToCategory(path) {
      const routeData = this.$router.resolve({path: path})
      window.open(routeData.href, '_blank')
    },

    //随机颜色
    randomColor() {
      let letters = "0123456789ABCDEF";
      let color = "#";
      do {
        for (let i = 0; i < 6; i++) {
          color += letters[Math.floor(Math.random() * 16)];
        }
      } while (color === "#FFFFFF" || color === "#000000");
      return color;
    },

    // 查询网站配置信息
    fetchWebsiteConfig() {
      getWebinfo().then(res => {
        if (res.code === 200) {
          this.config = res.data;
        }
      });
    },

    getViewArticle() {
      getViewsArticle().then(res => {
        if (res.code === 200) {
          this.viewsArticleList = res.data
        }
      })
    },

    // 查询文章列表信息
    fetchArticleList() {
      const param = {
        pageNo: this.current,
        pageSize: this.size
      };
      getArticleList(param).then(res => {
        if (res.code === 200) {
          this.articleList = res.data.list;
          this.total = res.data.size;
          const a = Math.ceil(res.data.total / this.size)
          if (a === 1) {
            this.moreTitle = "没有更多了..."
          }
        }
      });
    },

    screenshot() {
      this.$toast({ type: "error", message: "敬请期待!" });
    },

    closeDialog() {
      this.form.type = 1;
      this.form.email = null;
      this.form.title = null;
      this.form.content = null;
      this.$store.commit("setDialogFormVisible");
    },

    getHandlerArticleSummary(str) {
      return handlerArticleSummary(str)
    },

    getHandlerArticleTitle(str) {
      return handlerArticleTitle(str)
    },

    // 初始化
    init() {
      // 获取文章列表
      this.fetchArticleList();
      this.fetchNoticeInfo();
      document.title = "西瓜博客-首页";
    },
    scrollDown() {
      window.scrollTo({
        behavior: "smooth",
        top: document.documentElement.clientHeight
      });
    },

    // 获取公告信息
    fetchNoticeInfo() {
      systemNotice().then(res => {
        if (res.code === 200) {
          this.noticeContent = res.data.noticeContent;
        }
      })
    },

    runTime() {
      var timeold = new Date().getTime() - new Date(1627747200000).getTime();
      var msPerDay = 24 * 60 * 60 * 1000;
      var daysold = Math.floor(timeold / msPerDay);
      var str = "";
      var day = new Date();
      str += daysold + "天";
      str += day.getHours() + "时";
      str += day.getMinutes() + "分";
      str += day.getSeconds() + "秒";
      this.time = str;
    },

    /*open: function() {
      // 发送心跳消息
      var that = this;
      that.heartBeat = setInterval(function() {
        that.send("ping");
      }, 30 * 1000);
    },
    error: function() {},
    getMessage: function(event) {
      this.onlineCount = event.data;
    },
    send(message) {
      this.socket.send(message);
    },
    close: function() {}*/
  },
  computed: {
    isRight() {
      return function(index) {
        if (index % 2 === 0) {
          return "article-cover left-radius";
        }
        return "article-cover right-radius";
      };
    },
    /*blogInfo() {
      return this.$store.state.blogInfo;
    },
    isShowSocial() {
      return function(social) {
        return (
          this.$store.state.blogInfo.webSite.showList.indexOf(social) != -1
        );
      };
    },*/
    cover() {
      return "background: url(" + this.config.backgroundImage + ") center center / cover no-repeat";
    }
  }
};
</script>

<style lang="stylus">


.typed-cursor
  opacity: 1
  animation: blink 0.7s infinite
@keyframes blink
  0%
    opacity: 1
  50%
    opacity: 0
  100%
    opacity: 1
</style>

<style scoped>
.home-banner {
  position: absolute;
  top: -60px;
  left: 0;
  right: 0;
  height: 100vh;
  background-attachment: fixed;
  text-align: center;
  color: #fff !important;
  animation: header-effect 1s;
}
.banner-container {
  margin-top: 43vh;
  line-height: 1.5;
  color: #eee;
}
.blog-contact a {
  color: #fff !important;
}
.card-info-social {
  line-height: 40px;
  text-align: center;
  margin: 6px 0 -6px;
}
.card-info-social a {
  font-size: 1.5rem;
}
.left-radius {
  border-radius: 8px 0 0 8px !important;
  order: 0;
}
.right-radius {
  border-radius: 0 8px 8px 0 !important;
  order: 1;
}
.article-wrapper {
  font-size: 14px;
}
@media (min-width: 760px) {
  .blog-title {
    font-size: 2.5rem;
  }
  .blog-intro {
    font-size: 1.5rem;
  }
  .blog-contact {
    display: none;
  }
  .home-container {
    max-width: 1200px;
    margin: calc(100vh - 48px) auto 28px auto;
    padding: 0 5px;
  }
  .article-card {
    display: flex;
    align-items: center;
    height: 280px;
    width: 100%;
    margin-top: 20px;
  }
  .article-cover {
    overflow: hidden;
    height: 100%;
    width: 45%;
  }
  .on-hover {
    transition: all 0.6s;
  }
  .article-card:hover .on-hover {
    transform: scale(1.1);
  }
  .article-wrapper {
    padding: 0 2.5rem;
    width: 55%;
  }
  .article-wrapper a {
    font-size: 1.5rem;
    transition: all 0.3s;
  }
}
@media (max-width: 759px) {
  .blog-title {
    font-size: 26px;
  }
  .blog-contact {
    font-size: 1.25rem;
    line-height: 2;
  }
  .home-container {
    width: 100%;
    margin: calc(100vh - 66px) auto 0 auto;
  }
  .article-card {
    margin-top: 1rem;
  }
  .article-cover {
    border-radius: 8px 8px 0 0 !important;
    height: 230px !important;
    width: 100%;
  }
  .article-cover div {
    border-radius: 8px 8px 0 0 !important;
  }
  .article-wrapper {
    padding: 1.25rem 1.25rem 1.875rem;
  }
  .article-wrapper a {
    font-size: 1.25rem;
    transition: all 0.3s;
  }
}
.scroll-down {
  cursor: pointer;
  position: absolute;
  bottom: 0;
  width: 100%;
}
.scroll-down i {
  font-size: 3rem;
}
.article-wrapper a:hover {
  color: #8e8cd8;
}
.article-info {
  font-size: 95%;
  color: #858585;
  line-height: 2;
  margin: 0.375rem 0;
}
.article-info a {
  font-size: 95%;
  color: #858585 !important;
}
.article-content {
  line-height: 2;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}
.blog-wrapper {
  position: sticky;
  top: 10px;
}
.blog-card {
  line-height: 2;
  padding: 1.25rem 1.5rem;
}
.author-wrapper {
  text-align: center;
}
.blog-info-wrapper {
  display: flex;
  justify-self: center;
  padding: 0.875rem 0;
}
.blog-info-data {
  flex: 1;
  text-align: center;
}
.blog-info-data a {
  text-decoration: none;
}
.collection-btn {
  text-align: center;
  z-index: 1;
  font-size: 14px;
  position: relative;
  display: block;
  background-color: #49b1f5;
  color: #fff !important;
  height: 32px;
  line-height: 32px;
  transition-duration: 1s;
  transition-property: color;
}
.collection-btn:before {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: -1;
  background: #ff7242;
  content: "";
  transition-timing-function: ease-out;
  transition-duration: 0.5s;
  transition-property: transform;
  transform: scaleX(0);
  transform-origin: 0 50%;
}
.collection-btn:hover:before {
  transition-timing-function: cubic-bezier(0.45, 1.64, 0.47, 0.66);
  transform: scaleX(1);
}
.author-avatar {
  transition: all 0.5s;
}
.author-avatar:hover {
  transform: rotate(360deg);
}
.web-info {
  padding: 0.25rem;
  font-size: 0.875rem;
}
.scroll-down-effects {
  color: #eee !important;
  text-align: center;
  text-shadow: 0.1rem 0.1rem 0.2rem rgba(0, 0, 0, 0.25);
  line-height: 1.5;
  display: inline-block;
  text-rendering: auto;
  -webkit-font-smoothing: antialiased;
  animation: scroll-down-effect 1.5s infinite;
}
@keyframes scroll-down-effect {
  0% {
    top: 0;
    opacity: 0.4;
    filter: alpha(opacity=40);
  }
  50% {
    top: -16px;
    opacity: 1;
    filter: none;
  }
  100% {
    top: 0;
    opacity: 0.4;
    filter: alpha(opacity=40);
  }
}
.big i {
  color: #f00;
  animation: big 0.8s linear infinite;
}
@keyframes big {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.2);
  }
}
.guanzhu {
  margin-top: 10px;
}
.guanzhu .qqGroup {
  background: url()
    no-repeat;
  background-size: 22px;
  background-position-y: 47%;
}
.guanzhu .qq {
  background: url()
    no-repeat 0 -98px;
}
.guanzhu .email {
  background: url()
    no-repeat 0 -150px;
}

.guanzhu .gitee {
  background: url()
    no-repeat;
  background-size: 25px;
  background-position-y: 47%;
}
.guanzhu ul li {
  font-size: 12px;
  margin-bottom: 10px;
  background: #fff;
  color: #525252;
  line-height: 40px;
  padding: 0 0 0 34px;
  border: 1px solid #ddd;
  border-radius: 2px;
  position: relative;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}

.right-container {
  padding: 20px 24px;
  font-size: 14px;
}

.right-title {
  display: flex;
  align-items: center;
  line-height: 2;
  font-size: 16.8px;
  margin-bottom: 6px;
}

.card-title {
  padding-left: 40px;

  &::before,
  &::after {
    position: absolute;
    width: 12px;
    height: 12px;
    transform: none;
    background: #fc625d;
    border-radius: 50%;
    top: 4px;
    left: 20px;
    content: "";
  }

  &::after {
    left: 5px;
    background-color: #fdbc40;
  }
}

.article-item {
  display: flex;
  align-items: center;
  padding: 6px 0;
}

.article-item:first-child {
  padding-top: 0;
}

.article-item:last-child {
  padding-bottom: 0;
}

.article-item:not(:last-child) {
  border-bottom: 1px dashed #f5f5f5;
}

.article-item img {
  width: 100%;
  height: 100%;
  transition: all 0.6s;
  object-fit: cover;
}

.article-item img:hover {
  transform: scale(1.1);
}

.content {
  flex: 1;
  padding-left: 10px;
  word-break: break-all;
  display: -webkit-box;
  overflow: hidden;
  -webkit-box-orient: vertical;
}

.content-cover {
  width: 58.8px;
  height: 58.8px;
  overflow: hidden;
}

.content-title a {
  transition: all 0.2s;
  font-size: 95%;
}

.content-title a:hover {
  color: #2ba1d1;
}

.content-time {
  color: #858585;
  font-size: 85%;
  line-height: 2;
}

.right-title {
  display: flex;
  align-items: center;
  line-height: 2;
  font-size: 16.8px;
  margin-bottom: 6px;
}

.showInline {
  overflow:hidden;
  text-overflow:ellipsis;
  white-space:nowrap
}
</style>
